<?php

namespace app\data\controller\api\auth;

use app\data\controller\api\Auth;
use app\data\service\UserService;
use think\facade\Db;

/**
 * 会员资料管理
 * Class Center
 * @package app\data\controller\api\auth
 */
class Center extends Auth
{
    /**
     * 绑定数据表
     * @var string
     */
    protected $table = 'DataMember';

    /**
     * 更新会员资料
     * @throws \think\db\exception\DbException
     */
    public function set()
    {
        /*$data = $this->_vali([
            'headimg.default'       => '',
            'username.default'      => '',
            'base_age.default'      => '',
            'base_sex.default'      => '',
            'base_height.default'   => '',
            'base_weight.default'   => '',
            'base_birthday.default' => '',
        ]);*/
        $data = input('post.');
        foreach ($data as $key => $vo) if ($vo === '') unset($data[$key]);
        if (empty($data)) $this->error('没有需要修改的数据！');
        if ($this->app->db->name($this->table)->where(['id' => $this->mid])->update($data) !== false) {
            $this->success('更新会员资料成功！', $this->getMember());
        } else {
            $this->error('更新会员资料失败！');
        }
    }

    /**
     * 获取会员资料
     */
    public function get()
    {
        $userinfo = $this->getMember();
        //足迹
        $userinfo['track'] = $this->app->db->name('DataNewsXHistory')->where(['mid' => $this->mid])->count();
        //点赞
        $userinfo['like'] = Db::name('data_member_like')->where(['user_id' => $this->mid])->count();
        //获赞
        $userinfo['praise'] = Db::name('data_member_like')
            ->alias('a')
            ->join('data_news_item b','a.a_id=b.id')
            ->where('a.user_id',$this->mid)
            ->count();
        //作品
        $userinfo['mywork'] = Db::name('data_news_item')->where('publisher='.$this->mid)->count();
        //评论
        $userinfo['comment'] = Db::name('data_news_x_comment')->where('mid='.$this->mid)->count();
        //消息
        $userinfo['message'] = Db::name("data_user_message")->where('user_id='.$this->mid)->count();
        //草稿箱
        $userinfo['drafts'] = Db::name("data_news_item_drafts")->where('publisher='.$this->mid)->count();
        $this->success('获取会员资料', $userinfo);
    }

    /**
     * 获取会员数据统计
     */
    public function total()
    {
        $this->success('获取会员数据统计!', UserService::instance()->total($this->mid));
    }

    /**
     * 绑定会员邀请人
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function bindFrom()
    {
//        $data = $this->_vali(['from.require' => '邀请人不能为空']);
        $data = input('post.');
        if ($data['from'] == $this->mid) {
            $this->error('邀请人不能是自己哦', UserService::instance()->total($this->mid));
        }
        $from = $this->app->db->name($this->table)->where(['id' => $data['from']])->find();
        if (empty($from)) $this->error('邀请人状态异常', UserService::instance()->get($this->mid));
        if ($this->member['from'] > 0) $this->error('您已经绑定了邀请人', UserService::instance()->total($this->mid));
        if ($this->app->db->name($this->table)->where(['id' => $this->mid])->update($data) !== false) {
            $this->success('绑定邀请人成功！', UserService::instance()->total($this->mid));
        } else {
            $this->error('绑定邀请人失败！', UserService::instance()->total($this->mid));
        }
    }

    /**
     * 获取我邀请的朋友
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getFrom()
    {
        $query = $this->_query($this->table);
        $query->where(['from' => $this->mid])->field('id,from,username,nickname,headimg,create_at');
        $this->success('获取我邀请的朋友', $query->order('id desc')->page(true, false, false, 15));
    }

    /**
     * 修改用户信息
     * @throws \think\Exception
     * @throws \think\db\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function editMember(){
        $data = input();
        if (!empty($data)){
            $result = $this->app->db->name($this->table)->where('id='.$this->mid)->update($data);
            if ($result !== false){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{
            $this->error('无修改');
        }
    }


    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 我的作品
     */
    public function myWorks(){
        $data = input();
        $where = [
//            'form'      => $data['form'],
            'publisher' => $this->mid,
            'a.deleted'   => 0,
            'a.status'    => 1
        ];
        if (!empty($data['form'])) $where['form'] = $data['form'];
        $list = Db::name('data_news_item')
            ->alias('a')
            ->join('data_member b','a.publisher=b.id')
            ->field('a.*,b.username,b.headimg')
            ->where($where)
//            ->field('id,code,name,mark,remark,content,num_collect,create_at')
            ->paginate(sysconf('listrow'))
            ->toArray();

        foreach ($list['data'] as $key => $val){
            $val['like'] = $this->app->db->name('data_member_like')->where(['a_id' => $val['id'],'user_id' => $this->mid])->field('')->value('id');
            $list['data'][$key]['images'] = json_decode($val['images']);
            if (!empty($val['like'])){
                $list['data'][$key]['m_like'] = 1;
            }else{
                $list['data'][$key]['m_like'] = 2;
            }
        }

        if (!empty($list['data'])){
            $this->success('获取成功',$list);
        }elseif ($list !== false){
            $this->error('没有更多了');
        }else{
            $this->error('获取失败');
        }
    }

    /**
     * 我的点赞
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function myLike(){
        $list = $this->app->db->name('data_member_like')
            ->alias('a')
            ->join('data_news_item b','a.a_id=b.id')
            ->join('data_member c','a.user_id=c.id')
//            ->field('b.id,b.content,b.num_like,b.cover,b.images,b.video,c.username,c.headimg')
            ->field('b.*,c.username,c.headimg')
            ->where(['user_id' => $this->mid])
            ->paginate(sysconf('listrow'))
            ->toArray();
//        dump($this->mid);
        /*foreach ($list['data'] as $key => $val){
            if (empty($val['image']) && empty($val['video'])){
                $list['data'][$key]['type'] = 1;
            }elseif (!empty($val['image'])){
                $list['data'][$key]['type'] = 2;
            }else{
                $list['data'][$key]['type'] = 3;
            }
        }*/

        foreach ($list['data'] as $key => $val) {
            $val['like'] = $this->app->db->name('data_member_like')->where(['a_id' => $val['id'], 'user_id' => $this->mid])->value('id');
            if (!empty($val['like'])) {
                $list['data'][$key]['m_like'] = 1;
            } else {
                $list['data'][$key]['m_like'] = 2;
            }
        }

        if (!empty($list['data'])){
            $this->success('获取成功',$list);
        }elseif ($list !== false){
            $this->error('没有更多了');
        }else{
            $this->error('获取失败');
        }
    }

    /**
     * 我的评论
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function myComment(){
        $list = $this->app->db->name('data_news_x_comment')
            ->alias('a')
            ->join('data_news_item b','a.cid=b.id')
            ->join('data_member c','a.mid=c.id')
            ->field('a.create_at,b.cover,b.name,c.username,c.headimg,a.content,b.name,b.cover')
            ->order('a.create_at desc')
            ->where('mid='.$this->mid)
            ->paginate(sysconf('listrow'));
        if ($list){
            $this->success('获取成功',$list);
        }else{
            $this->error('获取失败');
        }
    }

    /**
     * 用户信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function userMessage(){
        $list = $this->app->db->name('data_user_message')
            ->alias('a')
            ->join('data_news_item b','a.cid=b.id')
            ->join('data_member c','c.id=a.user_id')
            ->field('b.name,c.username,a.content')
            ->where('b.publisher='.$this->mid)
            ->paginate(sysconf('listrow'))
            ->toArray();
        foreach ($list['data'] as $key => &$val){
            $val['content'] = $val['username'] . $val['content'];
        }
        if (!empty($list['data'])){
            $this->success('获取成功',$list);
        }elseif ($list !== false){
            $this->error('没有更多了');
        }else{
            $this->error('获取失败');
        }
    }

    /**
     * 我的获赞
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function myPraise(){
//        $uid = $this->mid;
        $list = $this->app->db->name('data_member_like')
            ->alias('a')
            ->join('data_news_item b','a.a_id=b.id')
            ->join('data_member c','a.user_id=c.id')
            ->field('c.username,c.headimg,c.id as mid,a.create_at,b.name,b.cover,b.id as cid,b.publisher,b.form')
            ->where('b.publisher='.$this->mid)
            ->paginate(sysconf('listrow'))
            ->toArray();
        if (!empty($list['data'])){
            $this->success('获取成功',$list);
        }elseif (empty($list['data'])){
            $this->error('没有更多了');
        }else{
            $this->error('获取失败');
        }
    }

    /**
     * 草稿箱
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function drafts(){
        $map = [
            'publisher'        => $this->mid
        ];
        $list = $this->app->db->name('data_news_item_drafts')
            ->alias('a')
            ->join('data_member b','a.publisher=b.id')
            ->where($map)
            ->paginate(sysconf('listrow'))
            ->toArray();
        if (!empty($list['data'])){
            $this->success('获取成功',$list);
        }elseif (empty($list['data'])){
            $this->error('没有更多了');
        }else{
            $this->error('获取失败');
        }
    }

    /**
     * 草稿箱内容 草稿箱投稿
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function draftsDetails(){
        $data = input();
        if (request()->isGet()){
            $result = Db::name("data_news_item_drafts")
                ->alias('a')
                ->join('data_member b ','a.publisher = b.id')
                ->field('a.*,b.username,b.headimg')
                ->order("a.create_at")
                ->where("a.id=".$data['id'])
                ->find();
            if ($result['publisher'] !== $this->mid){
                $this->error('暂无权限');
            }
            if ($result){
                $this->success('获取成功',$result);
            }else{
                $this->error('获取失败');
            }
        }else{
            $arr = [
                'name'          => $data['name'],
                'content'       => $data['content'],
                'mark'          => $data['mark'],
                'cover'         => $data['cover'],
                'cate_id'       => $data['cate_id'],
//                'images'        => $data['images'],
//                'video'         => $data['video'],
                'form'         => $data['form'],
                'type'         => $data['type'],
                'publisher'         => $data['publisher']
            ];

            if (!empty($data['images'])) $arr['images'] = $data['images'];
            if (!empty($data['video'])) $arr['video'] = $data['video'];

            Db::startTrans();
            $res = Db::name("data_news_item")->insert($arr);
            $del = Db::name("data_news_item_drafts")->where('id='.$data['id'])->delete();
            if ($res && $del){
                Db::commit();
                $this->success('上传成功');
            }else{
                Db::rollback();
                $this->error('上传失败');
            }
        }


//        Db::name('')
    }

}